package com.javajun.endofterm.controller.api_v1;

import com.javajun.endofterm.common.AjaxUtils;
import com.javajun.endofterm.common.DataGridParam;
import com.javajun.endofterm.common.DataGridUtils;
import com.javajun.endofterm.entity.Domitory;
import com.javajun.endofterm.entity.PowerRecord;
import com.javajun.endofterm.service.DomitoryService;
import com.javajun.endofterm.service.PowerRecordService;
import com.javajun.endofterm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description: 电费记录API
 * Author: Xiong Limin
 * Date: 2020-12-02-9:39 下午
 */
@RequestMapping("/api-v1/powerRecord")
@RestController
public class PowerRecordController {

    @Autowired
    private PowerRecordService powerRecordService;

    @Autowired
    private DomitoryService domitoryService;

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/page")
    private Object page(DataGridParam param) {
        if (param.getLimit() == null) {
            param.setLimit(15);
        }
        if (param.getPage() == null) {
            param.setPage(1);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Pageable pageable = param.getPageable(sort);
        Page<PowerRecord> page = powerRecordService.findAll(pageable);

        /**
         * 学生用户登录,只返回当前用户所在的宿舍的快递信息
         */
        boolean flag = studentService.whetherStudent();
        if (flag) {
            int domitoryId = studentService.getLodStudentDomitoryId();
            Page<PowerRecord> exp = powerRecordService.findByDomitoryId(pageable, domitoryId);
            return DataGridUtils.buildResult(exp);
        }
        return DataGridUtils.buildResult(page);
    }

    @RequestMapping(value = "/add")
    public Object addPowerRecharge(Integer domId, Double usePower) {
        Domitory domitory = domitoryService.findById(domId);
        domitory.setSurplusPower(domitory.getSurplusPower() + usePower);
        PowerRecord powerRecord = new PowerRecord(domitory, usePower, "充值");
        domitory.getPowerRecordList().add(powerRecord);
        domitoryService.save(domitory);
        powerRecordService.save(powerRecord);
        return AjaxUtils.buildResult("充值成功");
    }

    @RequestMapping(value = "/delete")
    public Object deletePower(Integer id) {
        PowerRecord powerRecord = powerRecordService.findById(id);
        Domitory domitory = powerRecord.getDomitory();
        if (domitory != null) {
            domitory.getPowerRecordList().remove(powerRecord);
            domitoryService.save(domitory);
        }
        powerRecordService.delete(powerRecord);
        return AjaxUtils.buildResult("删除成功");
    }

}
